from PIL import Image, ImageDraw, ImageFont, ImageFilter
import sys, os, random

class  picture_edit():
    def __init__(self,imagePath):
        self.imagePath = imagePath
        self.Im = Image.open(self.imagePath)
        self.L, self.R = self.imagePath.rsplit(".", 1)
        self.w, self.h = self.Im.size
        self.font = ImageFont.truetype('Arial.ttf', min(self.w // 6, self.h // 6))
        self.draw = ImageDraw.Draw(self.Im)
        self.num = str(random.randint(1, 99))
        self.rndcolor = (random.randint(32,127),random.randint(32,127),random.randint(32,128))
        self.rndchar = chr(random.randint(64,90))

    def add_num(self,Snum="42"):

        self.draw.text((self.w*0.81,self.h*0.75),Snum,font=self.font,fill="red")

        newfile = self.L + "_" + Snum + "."+ self.R

        self.Im.save(newfile,"png")

    def aspect(self,postion):
        self.Im.thumbnail((self.w//postion,self.h//postion))
        filename = self.L + "_postion"+ "."+self.R
        self.Im.save(filename,"png")

    def blurry(self):
        im2 = self.Im.filter(ImageFilter.BLUR)
        filename = self.L+"_blurry"+"."+self.R
        im2.save(filename,"jpg")

    def create_verification(self):
        neww = 60*4
        newH = 60
        image = Image.new("RGB",(neww,newH),(255,255,255))
        newfont = ImageFont.truetype("Arial.tff",36)
        for x in range(newH):
            for y in range(neww):
                self.draw.point((x,y),fill=self.rndcolor)
        for i in range(4):
            self.draw.text((60*i+10,10),self.rndchar,font=newfont,fill=self.rndcolor)
        image = image.filter(ImageFilter.BLUR)
        image.save("code.jpg","jpeg")

    def create_ellipse(self):
        self.draw.ellipse((self.w*0.8,self.h*0.03,self.w*0.8+190,self.h*0.03+190),fill="red",outline="red")
        self.draw.text((self.w*0.81,self.h*0.03),self.num,font=self.font,fill='white')
        filename2 =self.L +"_ellipse1"+ "." + self.R
        self.Im.save(filename2)

class create_code():

    def __init__(self,w,h):
        self.w = w*4
        self.h = h
        self.image = Image.new("RGB",(self.w,self.h),(255,255,255))
        self.font = ImageFont.truetype("Arial.ttf",36)
        self.draw = ImageDraw.Draw(self.image)

    def __rndchar(self):
        return chr(random.randint(65,90))

    def __rndcolor(self):
        return (random.randint(64,255),random.randint(64,255),random.randint(64,255))

    def __rndcolor2(self):
        return (random.randint(32,127),random.randint(32,127),random.randint(32,127))

    def create_verification(self):
        for x in range(self.w):
            for y in range(self.h):
                self.draw.point((x, y), fill=self.__rndcolor())
        for i in range(4):
            self.draw.text((60 * i + 10, 10), self.__rndchar(), font=self.font, fill=self.__rndcolor2())
        self.image =self.image.filter(ImageFilter.BLUR)
        self.image.save("code.jpg", "jpeg")